# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import re
import time
import random
from pymongo import MongoClient

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
通过微信公众平台获取公众账号信息
素材管理 ---- 新建图文素材 ---- 超链接（此处获取文章信息）
getMoreInfo 获取阅读量，依据PC微信
get_comments 获取评论信息，依据PC微信
注：key是需要必修修改的 https://mp.weixin.qq.com/mp/getappmsgad 通过PC端获取参数key
fakeid 公众号ID 需要登录自己公众号获取
"""


class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "authority": "mp.weixin.qq.com",
            "method": "GET",
            "path": "/cgi-bin/appmsg?action=list_ex&begin=0&count=5&fakeid=MjM5NTA5NzYyMA==&type=9&query=&token=100231199&lang=zh_CN&f=json&ajax=1",
            "scheme": "https",
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            "dnt": "1",
            "referer": "https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=10&token=100231199&lang=zh_CN",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
            "x-requested-with": "XMLHttpRequest",
            # "cookie":"rewardsn=; wxtokenkey=777; ua_id=ArdbIBuMdLXIgO6aAAAAANbeyg14ST1DELAef-vruM0=; pgv_pvi=554907648; pgv_si=s7852503040; uuid=e1f61abb5978f8a7bb4dc720f5fd7543; ticket=c14fff06a90f853cb0a5287bace8bb65ae137ed7; ticket_id=gh_9400a633deba; cert=BO__dbNLV9B9XdN30yUKqEEmE5ZptS0N; noticeLoginFlag=1; rand_info=CAESIMfWFZxBf5T4W6Dh11Jmb/DeeVha+VKR/dcS7UkA+r0p; slave_bizuin=3228838535; data_bizuin=3540058297; bizuin=3228838535; data_ticket=io9BsKvK2uvrC16iBxAoKhVRowW/qq7FEMdODBjiv9c3qONnGQE+8JA9pkZGXdpu; slave_sid=SWoyc2VoQkVzUkx1OVJFc2ZNbTJhNzNhUjhrTnhual9kVEx1ZlVlNDhxSkVQYlJROHdYb2l1czdDSUwwbkdGcllNNlJrdkVjdlg2blY3OVQ2akNaU25ONHBXN2kweV9ZZHRuQTdSSllISEhQajhGTzhXU0tFT1p3dm85Mmh0Y3ZGbDRoY01la0xXdW04MUdX; slave_user=gh_9400a633deba; xid=bc8f5a5e4561888ca319ee29e0c62b51; openid2ticket_ouQAB0-nuUl7uQVKi3Le9fUs5KHY=mwuZwMNu04GnpJulFtFVbZDudxPfv1ublwqCCNr6AnE=; mm_lang=zh_CN"
            "cookie":"ua_id=QM13JVMw87tlbYSPAAAAAKnqr0vfNKLYGqvJHlPdnj0=; pgv_pvi=9729862656; pgv_si=s9486158848; uuid=ab2de507ddcc3dff8fcc78ccef72326e; ticket=06fdf987eac0d578776611b6755ee664de4582d9; ticket_id=gh_9400a633deba; cert=lJUCAIXyOHzcBHOnF2US4suK5jWDQ_kE; noticeLoginFlag=1; rand_info=CAESIGpC7NOsffx1O1KjZLupNyAzO68Ju0ShCaE501JSP3rG; slave_bizuin=3228838535; data_bizuin=3540058297; bizuin=3228838535; data_ticket=WT+TiAQ5RLIKcaLvZVkKrs2TYn9+pRmlQjz2onKTVvF5UgvbzfUkn47NyHzZMLxl; slave_sid=cHlMc3FyOW5jOW4wazI3a1hNSHpxWjBwVDhjbFJobnl1RENBY0R0a0hoTlJYOHU0RW9scFRQRzM5Smt2N3FFZDZQY2ROOHVEdFRKUE1DU3gyckJSWkVmdlF3eldwa2tPRmgybGNuSUFWaV94eDlWelJLWFk2NklpdTlXVU5udEZ4VTZiMGcxTVpLem9XQWZi; slave_user=gh_9400a633deba; xid=2fdf24a3bc8f5b4d72616abb5c36b1f6; openid2ticket_ouQAB0-nuUl7uQVKi3Le9fUs5KHY=6xvHKff6dopdjynyKu9QMlzfmTdqX2k0+tE4sHC8yxQ=; mm_lang=zh_CN"
        }
        # 文章阅读数量、在看数量-通过微信pc端抓包获取
        self.key = "9a4d146448ba95b4288e7a8b3ec55657f1b584f759b1afa5c1aea9154fc86c8059f9d7fcd4674ac21730357939688d5aa18d4b749368c2b486fdb14d65ef77b1207ab0b2771e043727f104e2ff3deb10"
        # self.fakeid = "MjM5NTA5NzYyMA=="  # 公众号ID 上海发布 MjM5NTA5NzYyMA==
        self.fakeid = "MjM5Njg4OTA0Mg=="  # 公众号ID 上海发布 MjM5NTA5NzYyMA==
        self.token = "1154893577"  # 自己的token
        self.gzh_id = "shanghaifabu"
        self.gzh_name = "上海发布"


    def get_contents(self, begin):
        postData = {
            'action': 'list_ex',
            'begin': begin,  # 起始偏移量0  每次递增5
            'count': '5',
            'fakeid': self.fakeid,  #"MjM5NTA5NzYyMA==",  # 公众号标识
            'type': '9',
            'query':'',
            'token': self.token,
            'lang': 'zh_CN',
            'f': 'json',
            'ajax': '1'
        }
        print(f'=========offset：{begin}====={datetime.now().strftime("%Y-%m-%d %H:%M:%S")}=======')
        url = "https://mp.weixin.qq.com/cgi-bin/appmsg"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        count = data_json['app_msg_cnt']  # 总数量
        for d in data_json['app_msg_list']:
            dict_data = dict()
            dict_data['gzh_id'] = self.gzh_id
            dict_data['gzh_name'] = self.gzh_name
            dict_data['appmsgid'] = d['appmsgid']  # 文章ID
            dict_data['title'] = d['title']  # 标题
            dateArray = datetime.fromtimestamp(int(d['create_time']))  # 时间
            dict_data['create_time'] = dateArray.strftime("%Y-%m-%d %H:%M:%S")
            dateArray = datetime.fromtimestamp(int(d['update_time']))  # 更新时间
            dict_data['update_time'] = dateArray.strftime("%Y-%m-%d %H:%M:%S")
            dict_data['cover'] = d['cover']  # 图片链接
            dict_data['link'] = d['link']  # 链接

            self.getMoreInfo(d['link'], dict_data)
        time.sleep(random.randint(5, 8))
        if begin < count:
            self.get_contents(begin + 10)
        else:
            print("============结束==============")
            return

    # 获取阅读数和点赞数。查看是否有评论
    def getMoreInfo(self, link, dict_item):
        # 获得mid,_biz,idx,sn 这几个在link中的信息
        mid = link.split("&")[1].split("=")[1]
        idx = link.split("&")[2].split("=")[1]
        sn = link.split("&")[3].split("=")[1]
        biz = link.split("&")[0].split("_biz=")[1]
        pass_ticket = "%252Fw4ie7AJiJlhY2k0doBPlVkDeBMFq9cDVCWqrsbDeu5sYMTIl%252BFAvX2PMqMz4Zkh"
        appmsg_token = "1038_P%2Bn1EiLfpsod5rjtmKtHI_REloX9J8ztMSz8vmoLtEyHy5XBwA6A7cSrFdb2KTFaO2yPpuQCxPL042_V"
        url = "https://mp.weixin.qq.com/mp/getappmsgext"
        headers = {
            "Cookie": "pgv_info=ssid=s3753647948; pgv_pvid=1039986400; rewardsn=; wxtokenkey=777",
            # "Cookie": "ua_id=xwEUEdUTuvet91EAAAAAAOnJ2WF7Or7u5J52IVmQqVA=; pgv_pvi=3568088064; pgv_si=s7198294016; uuid=9d120d4f0013f9306cb3e1bb9bea31d0; ticket=3eef2f65d7e194938cf2ce5727c7a01eced5a0c9; ticket_id=gh_9400a633deba; cert=fEgAsUSZEEIA3cImsbDqGPf8wi9YbodK; noticeLoginFlag=1; rand_info=CAESILYisy6cKVwH6h17Oxtnj49EampokFtRUgXKIhPpbHg9; slave_bizuin=3228838535; data_bizuin=3540058297; bizuin=3228838535; data_ticket=U3YdltckKp8ODhTetfq4/iZVQ7/faCdmHw+EGilEEnkrZ8Wh0YuOShxnJSA8niQD; slave_sid=VUdub3QyZ1l1cXVaR1JWTlJNdkZ3UDVfblgyMTVrYU9ydFhtZVU5ZDVqa1V3blByZWFWN3Z3UE1JR3Zrb2dVRk4zWHU4X0hieHdtWk9pWldBbjRHX0Vhck5fUmN0MWM2MzhXMkVleVNhZk90OWJxMEwxbWVSTjJhcmxPelIwSXRoSkRndXZoRHlIcGJaSE41; slave_user=gh_9400a633deba; xid=d6fd79e9346c9f547515330e3a2d0b3a; openid2ticket_ouQAB0-nuUl7uQVKi3Le9fUs5KHY=B1Z5Z4iF/ClWXX7p+IA8o7r8VpqARt35TUtwPGgAi9s=; mm_lang=zh_CN",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.901.400 QQBrowser/9.0.2524.400",
            "Accept-Encoding": "gzip",
            "Content-Type": "application/x-www-form-urlencoded"
        }
        #query
        params = {
            "f":"json",
            "mock":"",
            "uin":"NTI3MjM5NTM1",
            "key":self.key,
            "pass_ticket":pass_ticket,
            "wxtoken":"777",
            "__biz":biz,#"MzI0ODcxODk5OA%3D%3D",
            "appmsg_token":appmsg_token,
            "x5":"0",
        }
        #body
        data = {
            "r":"0.12943889170223466",
            "__biz":biz,#"MzI0ODcxODk5OA%3D%3D",
            "appmsg_type":"9",
            "mid":mid,# "2247511098",
            "sn":sn,#"cd5d3b6941469f3d8b19d35931fb4b48",
            "idx":idx,
            "is_need_ticket":"0",
            "is_need_ad":"0",
            "is_need_reward":"0",
            "both_ad":"0",
            "reward_uin_count":"0",
            "msg_daily_idx":"1",
            "is_original":"0",
            "is_only_read":"1",
            "pass_ticket":pass_ticket,
            "is_temp_url":"0",
            "item_show_type":"0",
            "tmp_version":"1",
            "more_read_type":"0",
            "appmsg_like_type":"2",
        }
        time.sleep(random.randint(8, 10))
        html = requests.post(url, params=params, data=data, headers=headers, verify=False)
        data_json = json.loads(html.text)
        dict_item['readNum'] = data_json["appmsgstat"]["read_num"]  # 阅读数量
        dict_item['likeNum'] = data_json["appmsgstat"]["like_num"]  # 在看数量
        print("============文章信息==============")
        print(str(dict_item))
        # 获取command_id 在文章链接中
        headers_comment = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
        }
        time.sleep(2)
        html_command = requests.get(dict_item['link'], headers=headers_comment, verify=False)
        command_id = re.findall(r"var comment_id =(.*?);", html_command.text)
        # 如果有comment_id则是有评论
        if len(command_id) > 0:
            command_id = command_id[0].split("||")[0].replace("\"", "").strip()
            if len(command_id) > 1:
                time.sleep(2)
                # 获取评论内容
                self.get_comments(pass_ticket, biz, appmsg_token,dict_item['appmsgid'], command_id, dict_item)
        else:
            self.insertItem("微信公众号", dict_item)


    # 获取评论
    def get_comments(self, pass_ticket, biz, appmsg_token, appmsgid, command_id, dict_item):
        headers = {
            "Cookie": "pgv_info=ssid=s3753647948; pgv_pvid=1039986400; rewardsn=; wxtokenkey=777",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.901.400 QQBrowser/9.0.2524.400",
            "Accept-Encoding": "gzip",
            "Content-Type": "application/x-www-form-urlencoded",
        }
        params = {
            "action":"getcomment",
            "scene":"0",
            "idx":"1",
            "appmsgid":appmsgid,
            "comment_id":command_id,
            "offset":"0",
            "limit":"100",
            "uin":"NTI3MjM5NTM1",
            "key":self.key,#"e4c897b74c934688f80f0ce380fcc8bd706617b7f06112806dfc100898027479c0d34fbf06465bfba9aa3bc2d417d15caa2785f1ee31ef4bd93b4fcd172642390a98f185859c8b6ae114d2d10aeae967",
            "pass_ticket":pass_ticket,#"%25252Fw4ie7AJiJlhY2k0doBPlVkDeBMFq9cDVCWqrsbDeu5sYMTIl%25252BFAvX2PMqMz4Zkh&wxtoken:777",
            "__biz":biz,#"MjM5NTA5NzYyMA%3D%3D",
            "appmsg_token":appmsg_token,#"1038_lWmszn7fOWIcFFR5mKtHI_REloX9J8ztMSz8vjcDFHDTX21y_Qp1uZQGRKQWkt_60xLQnRRiB0lBxWrm",
            "x5":"0",
            "f":"json"
        }
        url = "https://mp.weixin.qq.com/mp/appmsg_comment"
        html_comment = requests.get(url, params=params, headers=headers, verify=False)
        print("============评论内容==============")
        data_json = json.loads(html_comment.text)
        dict_item['comment_count'] = data_json['elected_comment_total_cnt']  # 评论数量

        # for d in data_json["elected_comment"]:
        #     dict_data = dict()
        #     dict_data['nick_name'] = d["nick_name"]  # 昵称
        #     dict_data['content'] = d["content"]  # 内容
        #     dateArray = datetime.fromtimestamp(int(d['create_time']))  # 时间
        #     dict_data['create_time'] = dateArray.strftime("%Y-%m-%d %H:%M:%S")
        #     dict_data['like_num'] = d["like_num"]  # 点赞数量
        #     print(str(dict_data))

        self.insertItem("微信公众号", dict_item)

    def insertItem(self, tableName, data):
        my_set = db[tableName]
        my_set.insert_one(data)

if __name__ == '__main__':
    conn = MongoClient('127.0.0.1', 27017)
    db = conn["Test"]
    test_get = Test_Get()
    # 由0开始，每次递增5
    test_get.get_contents(0)